package com.lxy_s_springboot.lxy_s_springboot.controller;

import com.lxy_s_springboot.lxy_s_springboot.model.MessageInfo;
import com.lxy_s_springboot.lxy_s_springboot.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
//    private List<MessageInfo> messageinfos = new ArrayList<>();
    @Autowired
    private MessageService messageService;
    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        log.info("I'm in");
        return messageService.getMessageInfo();
//        return messageinfos;
    }
    @RequestMapping(value = "/publish",produces = "application/json")
    public String publish(@RequestBody MessageInfo messageinfo){
        if(StringUtils.hasLength(messageinfo.getMessage())
        &&StringUtils.hasLength(messageinfo.getFrom())
                && StringUtils.hasLength(messageinfo.getTo())){
            //添加留言
            messageService.addMessage(messageinfo);
//            messageinfos.add(messageinfo);
            return "{\"ok\":1}";
        }
        return "{\"ok\":0}";
    }
}
